<?php 
session_start(); 
error_reporting(E_ALL);
require_once 'application/library/Betting_lib.php'; 
$bet = new Betting_lib();
?>
<!DOCTYPE html>
<html>
<head>
	<title>2Bet <?php echo $_SESSION['username']; ?></title>
	<!-- <script src=""></script> -->
	<link rel="stylesheet" type="text/css" href="css/style.css" />
	<script src="js/modernizr.custom.63321.js"></script>

</head>
<body>
	<div class="container">
		<br><br><br><br>
			<header>
				<h1><strong>2Bet</strong> betting</h1>
				<br><br>
				<div class="support-note">
					<span class="note-ie">Sorry, only modern browsers.</span>
				</div>	
			</header>
		<div class="content">
			<?php				
				if($_GET['submit_bid']){
					echo '<p style="margin:10px;padding:5px;border:3px solid #333;">';
						$bet->insert_bid($_GET['product_id'],$_GET['user']);
					echo '</p>';
				}
				
			?>
			<?php
			
			$result = $bet->get_products();
			
			echo '<strong>Available products</strong>';
			echo '<p>';
			while ($row = $result->fetch_object()) {
	        	echo 'ID:'.$row->id.', Name: '.$row->name.', Price: '.number_format($row->price,2,',','.').'<br>';
			}
			echo '</p>';
			
			$bid = $bet->get_bids(array('table'=>'bids',
										'fields'=>array('product_id','SUM(value) as value','timestamp'),
										'group'=>'product_id',
										'order'=>'timestamp desc',
										'limit'=> 20,
										'log'=>true
								));
			echo '<br>';
			echo '<strong>Bids placed</strong>';
			echo '<p>';
			while ($row = $bid->fetch_object()) {
	        	echo 'Product: '.$row->product_id.' with amount: '.$row->value.'<br>';
			}
			echo '</p>';
			
			$bid = $bet->get_bids(array('table'=>'bids','fields'=>array('product_id','user_id','value','timestamp'),'order'=>'timestamp desc','limit'=>20,'log'=>true));
			echo '<br>';
			echo '<strong>Bids history</strong>';
			echo '<p>';
			while ($row = $bid->fetch_object()) {
	        	echo 'A bid has been made by user_id: '.$row->user_id.' on product_id '.$row->product_id.' with amount: '.$row->value.' at '.date('H:i:s',$row->timestamp).'<br>';
			}
			echo '</p>';
			?>
			<br>
			<form action="" method="get">
				<label><strong>User placing the bid</strong></label><br>
				<select name="user">
					<?php
						echo '<option value="'.$_SESSION['userid'].'">'.$_SESSION['username'].'</option>';
						//echo '<option value="'.$row->user_id.'">'.$row->username.'</option>';
					?>
				</select>
				<input type="text" name="product_id" value="1" size="5">
				<input type="submit" name="submit_bid" value="Submit">
			</form>
			<br>
			<div class="logout">
				<a href="login-logout.php" title="Log user <?php echo $_SESSION['username'] ?> out"><strong>Log out</strong></a>
				<br><br><br>
			</div>
			<?php 
				if($_SESSION['data']) {
					echo 'Data returned from login-service on davidilag.dk:';
					$t = $_SESSION['data'];
					echo '<pre>';
					print_r($t);
					$_SESSION['data'] = NULL;
				}	
			?>
		</div>
	</div>

</body>
</html>